package com.inforich.report.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSON;
import com.inforich.common.config.InforichConfig;
import com.inforich.common.utils.DateUtils;
import com.inforich.common.utils.StringUtils;
import com.inforich.common.utils.file.FileUploadUtils;
import com.inforich.common.utils.uuid.Seq;
import com.inforich.report.domain.ResourceLandInfo;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

/**
 * @description: some desc
 * @author: HJM
 * @date: 2023/7/7
 */
public class ReportExcelHelper {

  private static final float TITLE_HEIGHT = 40f;
  private static final float ROW_HEIGHT = 30f;
  private static final int COLUMN_WIDTH = 6000;

  /**
   * 标题样式
   */
  private static CellStyle createTitleCellStyle(Workbook wb) {
    CellStyle titleCellStyle = wb.createCellStyle();
    Font titleCellFont = wb.createFont();
    titleCellFont.setBold(true);
    titleCellFont.setFontName("微软雅黑");
    titleCellFont.setFontHeightInPoints((short) 14);
    titleCellStyle.setFont(titleCellFont);
    titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
    titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    return titleCellStyle;
  }

  /**
   * 单元格label
   */
  private static CellStyle createLabelCellStyle(Workbook wb) {
    CellStyle labelCellStyle = wb.createCellStyle();
    Font labelCellFont = wb.createFont();
    labelCellFont.setBold(true);
    labelCellFont.setFontName("微软雅黑");
    labelCellFont.setFontHeightInPoints((short) 11);
    labelCellStyle.setFont(labelCellFont);
    labelCellStyle.setBorderBottom(BorderStyle.THIN);
    labelCellStyle.setBorderLeft(BorderStyle.THIN);
    labelCellStyle.setBorderTop(BorderStyle.THIN);
    labelCellStyle.setBorderRight(BorderStyle.THIN);
    labelCellStyle.setWrapText(true);
    labelCellStyle.setAlignment(HorizontalAlignment.CENTER);
    labelCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    return labelCellStyle;
  }

  /**
   * 单元格value
   */
  private static CellStyle createValueCellStyle(Workbook wb) {
    CellStyle cellStyle = wb.createCellStyle();
    Font cellFont = wb.createFont();
    cellFont.setFontName("微软雅黑");
    cellFont.setFontHeightInPoints((short) 11);
    cellStyle.setFont(cellFont);
    cellStyle.setBorderBottom(BorderStyle.THIN);
    cellStyle.setBorderLeft(BorderStyle.THIN);
    cellStyle.setBorderTop(BorderStyle.THIN);
    cellStyle.setBorderRight(BorderStyle.THIN);
    cellStyle.setWrapText(true);
    cellStyle.setAlignment(HorizontalAlignment.CENTER);
    cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    return cellStyle;
  }

  /**
   * 生成row
   */
  private static Row createRow(Sheet sheet, int rowNum, float height) {
    Row row = sheet.createRow(rowNum);
    row.setHeightInPoints(height);
    return row;
  }

  /**
   * 生成cell
   */
  private static void setCellValue(Row row, int column, CellStyle style, String value) {
    Cell cell = row.createCell(column);
    cell.setCellStyle(style);
    cell.setCellValue(value);
  }

  /**
   * cell设置为空
   */
  private static void setCellValueEmpty(HSSFRow row, CellStyle style, Integer... columns) {
    for (Integer column : columns) {
      HSSFCell cell = row.createCell(column);
      cell.setCellStyle(style);
      cell.setCellValue("");
    }
  }

  public static Workbook createLandInfo(List<ResourceLandInfo> list) {
    Workbook wb = new SXSSFWorkbook();
    Sheet sheet = wb.createSheet("资源化企业再生产品月度信息汇总表");
    sheet.setColumnWidth(0, COLUMN_WIDTH);
    sheet.setColumnWidth(1, COLUMN_WIDTH);
    sheet.setColumnWidth(2, COLUMN_WIDTH);
    sheet.setColumnWidth(3, COLUMN_WIDTH);
    sheet.setColumnWidth(4, COLUMN_WIDTH);
    sheet.setColumnWidth(5, COLUMN_WIDTH);
    CellStyle titleCellStyle = createTitleCellStyle(wb);
    CellStyle cellStyle = createValueCellStyle(wb);
    int rowNum = 0;
    Row row = createRow(sheet, rowNum, TITLE_HEIGHT);
    setCellValue(row, 0, titleCellStyle, "资源化企业再生产品月度信息汇总表");
    sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 0, 5));
    rowNum++;

    row = createRow(sheet, rowNum, ROW_HEIGHT);
    setCellValue(row, 0, cellStyle, "资源化企业");
    setCellValue(row, 1, cellStyle, "产品类别");
    setCellValue(row, 2, cellStyle, "产品种类");
    setCellValue(row, 3, cellStyle, "本月重量");
    setCellValue(row, 4, cellStyle, "累计重量");
    setCellValue(row, 5, cellStyle, "填报月份");
    rowNum++;
    if (CollUtil.isEmpty(list)) {
      return wb;
    }
    for (int i = 0; i < list.size(); i++) {
      ResourceLandInfo rli = list.get(i);
      row = createRow(sheet, rowNum, ROW_HEIGHT);
      setCellValue(row, 0, cellStyle, rli.getDisposalLandName());
      setCellValue(row, 1, cellStyle, rli.getSysRubbishTypeName());
      setCellValue(row, 2, cellStyle, rli.getSysRubbishTypeSubName());
      setCellValue(row, 3, cellStyle, String.valueOf(rli.getWeight()));
      setCellValue(row, 4, cellStyle, String.valueOf(rli.getTotalWeight()));
      setCellValue(row, 5, cellStyle, rli.getMonth());
      rowNum++;
    }

    // 企业名称合并
    String name = list.get(0).getDisposalLandName();
    ResourceLandInfo add = new ResourceLandInfo();
    add.setDisposalLandName("");
    add.setSysRubbishTypeName("");
    list.add(add);

    int startNo = 2;
    int calc = 0;
    for (int i = 0; i < list.size(); i++) {
      String name1 = list.get(i).getDisposalLandName();
      int endNo = startNo + calc - 1;
      if (!name1.equals(name)) {
        if (endNo > startNo) {
          sheet.addMergedRegion(new CellRangeAddress(startNo, endNo, 0, 0));
        }
        startNo = endNo + 1;
        name = name1;
        calc = 0;
      }
      calc++;
    }

    startNo = 2;
    calc = 0;
    name = list.get(0).getDisposalLandName() + ":" + list.get(0).getSysRubbishTypeName();
    for (int i = 0; i < list.size(); i++) {
      String name1 = list.get(i).getDisposalLandName()+ ":" + list.get(i).getSysRubbishTypeName();
      int endNo = startNo + calc - 1;
      if (!name1.equals(name)) {
        if (endNo > startNo) {
          sheet.addMergedRegion(new CellRangeAddress(startNo, endNo, 1, 1));
        }
        startNo = endNo + 1;
        name = name1;
        calc = 0;
      }
      calc++;
    }
    return wb;
  }

  /**
   * 上传excel
   */
  private static String uploadExcel(Workbook wb, String excelName) throws IOException {
    String filePath = InforichConfig.getUploadPath();
//    String filePath = "D:\\inforich\\uploadPath\\upload";
    String fileName = StringUtils.format("{}/{}_{}.{}", excelName, DateUtils.datePath(),
        Seq.getId(Seq.uploadSeqType), "xlsx");
    String absPath = FileUploadUtils.getAbsoluteFile(filePath, fileName).getAbsolutePath();
    System.out.println(absPath);
    try {
      FileOutputStream output = new FileOutputStream(absPath);
      wb.write(output);
      output.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return fileName;
  }

  public static void main(String[] args) throws IOException {
    String s = FileUtil.readString("D:\\111.txt", Charset.defaultCharset());
    List<ResourceLandInfo> list = JSON.parseArray(s, ResourceLandInfo.class);
    String res = uploadExcel(createLandInfo(list), "资源化企业再生产品月度信息汇总表");
    System.out.println(res);
  }

}
